package com.zh.note.huawei.twohundred.跳格子;


import java.util.*;

/**
 * https://dream.blog.csdn.net/article/details/129889758
 * 4
 * 1 2
 * 1 0
 * <p>
 * 5
 * 4 3
 * 0 4
 * 2 1
 * 3 2
 */
public class Main {
    public static List<List<Integer>> list = new ArrayList<>();

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int n = scanner.nextInt();
            boolean isValid = true;

            while (scanner.hasNext()) {
                int key = scanner.nextInt();

                if (key == -1) {
                    break;
                }
                int value = scanner.nextInt();
                if (key < 0 || key > n || value < 0 || value > n) {
                    System.out.println("error");
                    return;
                }
                List<Integer> childList = new ArrayList<>();
                childList.add(key);
                childList.add(value);
                list.add(childList);
            }
            //处理数据
            //都不合符条件，则返回true
            // isValid = list.stream()
            //         .noneMatch(childList -> isBinaryHeap(childList, list));
            // System.out.println(isValid ? "yes" : "no");
            for (int i = 0; i < list.size(); i++) {
                isValid = isBinaryHeap(list.get(i), list);
                if (isValid) {
                    System.out.println("no");
                    return;
                }
            }
            System.out.println("yes");
        }
    }
    // 判断是否存在循环依赖
    public static boolean isBinaryHeap(List<Integer> subList, List<List<Integer>> dataList) {
        Queue<Integer> queue = new LinkedList<>();
        Integer e = subList.get(subList.size() - 1);
        queue.offer(e);

        while (!queue.isEmpty()) {
            int first = queue.poll();

            for (List<Integer> list : dataList) {
                if (list.get(0) == first) {
                    int temp = list.get(1);
                    if (subList.contains(temp) || temp < 0 || temp > dataList.size()) {
                        return true;
                    }
                    subList.add(temp);
                    queue.offer(temp);
                }
            }
        }

        return false;
    }
}

